package com.bbs.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bbs.pojo.Admin;
import com.bbs.pojo.User;
import com.bbs.service.UserService;

@Controller
public class UserController {
	@Autowired
	private UserService userService;
	
	@RequestMapping("userregist")
	public String regist(@PathParam("username")String username,@PathParam("password")String password ,@PathParam("nickname")String nickname){
		System.out.println(username+password+nickname);
		User user=new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setNickname(nickname);
		userService.regist(user);
		return "forward:findUserAll";
	}
	/*
	 * ajax
	 */
	@RequestMapping("/ajax")
	@ResponseBody
	public void jiaoyan(HttpServletRequest req, HttpServletResponse resp, String username) throws Exception {
		List<User> userList = userService.findUsername(username);
		if (userList.size() != 0) {
			resp.getWriter().write("Failed");
		}else{
			resp.getWriter().write("Success");
		}
	}
	
	@RequestMapping("userlogin")
	@ResponseBody
	public String login(@PathParam("username")String username,@PathParam("password")String password){
		
		if("".equals(username)||"".equals(password)){
			return "用户名或密码不能为空";
		}
		if(username==null||password==null){
			return "用户名或密码不能为空";
		}
		System.out.println(username+password+"`````````````````````");
		User user=userService.login(username, password);
		if(user==null){
			return "用户名或密码不正确";
		}
		return "登陆成功";
	}
	/*
	 * 跳转到注册页面
	 * 
	 */
	@RequestMapping("/toregist")
	public String toregist(){
		
		return "/regist";
	}
	
}
